package com.voltmao.service.application.corp;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.voltmao.persistent.mapper.CorpEmployesUserMapper;
import com.voltmao.persistent.mapper.CorpOrganizationMapper;
import com.voltmao.persistent.model.CorpOrganization;
import com.voltmao.persistent.model.CorpOrganizationCriteria;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.utils.StringUtils;

import java.util.*;


/**
 * 企业组织管理服务接口
 *
 * @author zhanglin
 * @date 2018/04/06
 */
@Service
public class CorpOrganizationServiceImpl {
    private static final Logger testLogger = LoggerFactory.getLogger("MY_TEST_LOGGER");

    @Autowired
    private CorpOrganizationMapper corpOrganizationMapper;
    @Autowired
    CorpEmployesUserMapper corpEmployesUser;

    /**
     * 组织分页数据及查询组织
     * 为每个部门返回具体人数
     * @param params 查询参数map
     * @return DataPage集合
     */
    public DataPage selectOrgsDataPage(Map<String,Object> params){
        testLogger.info("待查询参数："+params.toString());
        DataPage dataPage = new DataPage();
        int pageIndex = StringUtils.parseInt(params.get("pageNumber")+"", 1);
        int pageSize = StringUtils.parseInt(params.get("pageSize")+"", 20);

        CorpOrganizationCriteria example = new CorpOrganizationCriteria();
        example.setPageSize(pageSize);
        example.setLimitStart((pageIndex-1) * pageSize);
        CorpOrganizationCriteria.Criteria c = example.createCriteria();
        c.andIsDelEqualTo(0);
        if(params.size() > 0){
            String dataId = "dataId";
            if(StringUtils.isNotEmpty(params.get(dataId))){
                //主键查询
                c.andDataIdEqualTo(params.get(dataId)+"");
            }
            String pDataId = "pDataId";
            if(StringUtils.isNotEmpty(params.get(pDataId))){
                //父节点查询
                c.andPDataIdEqualTo(params.get(pDataId)+"");
            }
            String name = "name";
            if(StringUtils.isNotEmpty(params.get(name))){
                //姓名模糊查询
                c.andNameLike("%" + params.get(name) + "%");
            }
            String des = "des";
            if(StringUtils.isNotEmpty(params.get(des))){
                //描述模糊查询
                c.andDesLike("%" + params.get(des) + "%");
            }
        }
        example.setOrderByClause("corp_organization.create_date desc");

        dataPage.pageindex= pageIndex;
        dataPage.pagesize = pageSize;

        dataPage.rows = corpOrganizationMapper.selectMapByExample(example);
        dataPage.total = corpOrganizationMapper.countByExample(example);

        return dataPage;
    }

    /**
     * 查询所有组织
     */
    public List<CorpOrganization> selectAllOrg(){
        CorpOrganizationCriteria example = new CorpOrganizationCriteria();
        CorpOrganizationCriteria.Criteria c = example.createCriteria();
        c.andIsDelEqualTo(0);
        example.setOrderByClause("create_date desc");
        return corpOrganizationMapper.selectByExample(example);
    }

    /**
     * 传进来对象
     * 新增或修改组织
     * 新增，对象不能传进来主键，即dataId
     * @param corpOrg 组织对象
     * @return 更新或新增记录条数
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateOrgByObject(CorpOrganization corpOrg){
        testLogger.info("新增或修改参数："+JSONObject.toJSONString(corpOrg));
        if(StringUtils.isEmpty(corpOrg.getDataId())){
            corpOrg.setCreateDate(new Date());
            corpOrg.setUpdateDate(new Date());
            corpOrg.setIsDel(0);
            corpOrg.setDataId(StringUtils.uuid36());
            //传进来的对象需要包括name,pdataid,des
            return corpOrganizationMapper.insertSelective(corpOrg);
        }else{
            //更新
            corpOrg.setUpdateDate(new Date());
            return corpOrganizationMapper.updateByPrimaryKeySelective(corpOrg);
        }
    }

    /*public int insertOrUpdateOrgByMap(Map<String,Object> params){
        System.out.println(params.toString());
        return 1;
    }

    public CorpOrganization selectOrgByPK(String pk){
        return corpOrganizationMapper.selectByPrimaryKey(pk);
    }
*/


}
